import { AppSetting } from '../AppSetting';
import { Container } from '../Container';
import { EventHandler, HelperTool } from './Common';
import { DataService } from "./DataService";
import { ViewerService } from "./ViewerService";
export class ModelManService {
    AppSetting = Container.defineProperty(AppSetting);
    ViewerService = Container.defineProperty(ViewerService);
    DataService = Container.defineProperty(DataService);
    UpdateChanged = new EventHandler();
    getData(): { modelLayers: ModelLayerInfo[] } {
        if (!this.DataService.Data.modelLayers) {
            this.DataService.Data.modelLayers = [];
        }
        return this.DataService.Data;
    }
    getModelInfo(model: Sippreep.Viewing.Model) {
        //@ts-ignore
        return model.tag as ModelLayerInfo;
    }
    getModel(info: ModelLayerInfo) {
        return this.ViewerService.viewer.getAllModels().find(m => this.getModelInfo(m) == info);
    }
    private async loadModel(item: { url: string, name: string }) {
        let model = await new Promise<Sippreep.Viewing.Model>((s, f) => {
            this.ViewerService.viewer.loadModel(HelperTool.getFullUrl(item.url,this.AppSetting.ViewerDataBaseUrl),
                { globalOffset: { x: 0, y: 0, z: 0 } }, m => { s(m); }
                , (errorCode, errorMessage) => {
                    f(errorMessage)
                });
        });
        //@ts-ignore
        model.loader.svfUrn = item.url;
        //@ts-ignore
        model.tag = item;
        return model;
    }
    async updateViewer() {
        let viewer = await this.ViewerService.ViewerPromise;
        let data = this.getData();
        let layers = [...data.modelLayers];
        viewer.getAllModels().forEach(model => {
            const index = layers.indexOf(this.getModelInfo(model));
            //@ts-ignore
            if (index < 0 || layers[index].url != model.loader.svfUrn)
                viewer.unloadModel(model);
            else
                layers.splice(index, 1);
        });
        let loadPromses = layers.map(async (item) => {
            return await this.loadModel(item);
        });
        await Promise.all(loadPromses);
        // let updatePromses = viewer.getAllModels().map(async model => {
        //     let item = this.getModelInfo(model);
        //     let tree = await new Promise<Sippreep.Viewing.InstanceTree>((s) => {
        //         model.getObjectTree(s);
        //     });
        //     let hide = item.hide == undefined ? false : item.hide;
        //     let transparent = item.transparent == undefined ? false : item.transparent;
        //     let rootId = tree.getRootId();
        //     model.visibilityManager.setNodeOff(rootId, hide);
        //     transparent ? model.visibilityManager.hide(rootId) : model.visibilityManager.show(rootId);
        // })
        // await Promise.all(updatePromses);
        //设置视角
        if (loadPromses.length > 0) {
            viewer.navigation.setView(new THREE.Vector3(1, -1, 1), new THREE.Vector3(0, 0, 0));
            viewer.fitToView();
        }
        this.UpdateChanged.dispatchEvent();
    }
}
export type ModelLayerInfo = { url: string, name: string, displayName?: string, hide?: boolean, transparent?: boolean };